﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace server.Tool
{
   public  class TimeUtility
    {

        public static long GetCurrentTimestamp()
        {
            TimeSpan ts = DateTime.Now - new DateTime(1970, 1, 1, 0, 0, 0, 0);
            long TotalSeconds = Convert.ToInt64(ts.TotalSeconds);
            return TotalSeconds;
        }

        public static long GetUTCTimeStamp()
        {
            TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);
            return Convert.ToInt64(ts.TotalSeconds);
        }


        public static string GetCurrentFormatTime()
        {
           // return DateTime.Now.ToString("hh:mm:ss");
           //return System.DateTime.Now.ToString("hh:mm:ss");
            return System.DateTime.Now.ToLongTimeString();
        }
        public static int ExecDateDiff(DateTime dateBegin, DateTime dateEnd)
        {
            TimeSpan ts1 = new TimeSpan(dateBegin.Ticks);
            TimeSpan ts2 = new TimeSpan(dateEnd.Ticks);
            TimeSpan ts3 = ts1.Subtract(ts2).Duration();
            //单位为秒
            return (int)(ts3.TotalMilliseconds / 1000);
        }

        /// <summary>
        /// 查询现在离开始时间是否少于nearMinutes分钟
        /// </summary>
        /// <param name="startTime"></param>
        /// <param name="nearMinutes"></param>
        /// <returns></returns>
        public static int GetStartTimeDiffer(TimeSpan startTime, int nearMinutes)
        {
            DateTime startDate = GetDateTimeByTimeSpan(startTime);

            if (DateTime.Compare(DateTime.Now, startDate) < 0)
            {
                int diffSeconds = ExecDateDiff(DateTime.Now, startDate);

                if (diffSeconds <= nearMinutes * 60)
                {
                    return diffSeconds;
                }
            }

            return -1;
        }

        /// <summary>
        /// /// <summary>
        /// 通过timeSpan和当前短时间构造当前日期
        /// </summary>
        /// <param name="item"></param>
        /// <returns></returns>
        /// </summary>
        public static DateTime GetDateTimeByTimeSpan(TimeSpan item)
        {
            string currentDateStr = DateTime.Now.ToString("yyyy-MM-dd");
            string _Time = currentDateStr + " " + item.Hours + ":" + item.Minutes + ":" + item.Seconds;
            DateTime date = Convert.ToDateTime(_Time);

            return date;
        }


        /// <summary>根据日期，获得星期几</summary>
        /// <param name="y">年</param>
        /// <param name="m">月</param>
        /// <param name="d">日</param>
        /// <returns>星期几，1代表星期一；7代表星期日</returns>
        /// 
        public static int GetWeek(int y, int m, int d)
        {
            if (m == 1) m = 13;
            if (m == 2) m = 14;
            int week = (d + 2 * m + 3 * (m + 1) / 5 + y + y / 4 - y / 100 + y / 400) % 7 + 1;
            return week;
        }

        public static string TimeSpanConverShortDateTime(long timestamp)
        {
            System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1));//当地时区
            DateTime time = startTime.AddSeconds(timestamp);
            return time.ToString("HH:mm:ss");
        }
        public static DateTime TimeSpanConverLongDateTime(long timestamp)
        {
            System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1));//当地时区
            DateTime time = startTime.AddSeconds(timestamp);
            return time;
        }

        /// <summary>
        /// 计算两个日期相差的分钟
        /// </summary>
        /// <param name="startTime"></param>
        /// <param name="endTime"></param>
        /// <returns></returns>
        public static double DiffMinutes(DateTime startTime, DateTime endTime)
        {
            TimeSpan minuteSpan = new TimeSpan(endTime.Ticks - startTime.Ticks);
            return minuteSpan.TotalMinutes;
        }

        public static int CompareDateTime(DateTime startTime, DateTime endTime)
        {
            int result = DateTime.Compare(startTime, endTime);
            return result;
        }

        
        /// <summary>
        /// 格式化
        /// </summary>
        /// <param name="millin"></param>
        /// <returns></returns>
        public static string FormatMillin(uint millin)
        {
            string _millin_timespan = millin.ToString();
            if (millin < 10)
            {

                _millin_timespan =  "00" + millin;
            }
            else if (millin < 100)
            {
                _millin_timespan = "0" + millin;
            }

            return _millin_timespan;
        }

    }
}
